package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.PayPassAdapter;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;
import com.wnhz.work.jy.view.ShowPopupWindow;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InPutPayPassWordActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private GridView gridView;
    private PayPassAdapter mAdapter;

    private String[] nums = {"1", "2", "3", "4", "5", "6", "7", "8", "9", " ", "0", ""};

    private String orderid;

    private TextView old_TextView1, old_TextView2, old_TextView3,
            old_TextView4, old_TextView5, old_TextView6;

    private View old_lay;

    private List<TextView> old_tv_list = new ArrayList<>();

    private List<String> list = new ArrayList<>();
    private int type;
    HashMap<String,Object> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_put_pay_pass_word);
        list.clear();
        initData();
        initView();
    }


    private void initData() {
        type = getIntent().getIntExtra("type",0);
        orderid = getIntent().getStringExtra("orderid");

    }

    private void initView() {
        gridView = (GridView) findViewById(R.id.gridView_pay);
        findViewById(R.id.tv_finish).setOnClickListener(this);
        findViewById(R.id.tv_true).setOnClickListener(this);

        old_lay = findViewById(R.id.old_lay);
        findViewById(R.id.wjmm).setOnClickListener(this);

        mAdapter = new PayPassAdapter(InPutPayPassWordActivity.this, nums);
        gridView.setAdapter(mAdapter);

        gridView.setOnItemClickListener(this);

        initOldTextview();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //取消
            case R.id.tv_finish:
                switch (type){
                    case 1:
                        startActivity(new Intent(InPutPayPassWordActivity.this, MyMain.class).putExtra("Pagination", 3));
                        break;
                    case 2:
                        finish();
                        break;

                }
                break;

            //确定
            case R.id.tv_true:

                switch (type){
                    case 1:
                        if (list.size() < 6){
                            MyToast("请输入支付密码");
                        }else {
                            uploadByxUtils();
                        }

                        break;
                    case 2:
                        if (list.size() < 6){
                            MyToast("请输入支付密码");
                        }else {
                            number = new StringBuffer("");
                            for (int i = 0; i < list.size(); i++) {
                                number.append(list.get(i));
                            }
                            Intent intent = new Intent();
                            intent.putExtra("passwork",number.toString());
                            setResult(RESULT_OK,intent);
                            finish();
                        }

                        break;
                }
                break;
            case R.id.wjmm:
                startActivity(new Intent(InPutPayPassWordActivity.this,PayPasswordActivity.class));
                break;
            default:
                break;
        }
    }

    public void MyToast(String s){
        Toast.makeText(getApplication(),s,Toast.LENGTH_SHORT).show();
    }
    private StringBuffer number = new StringBuffer("");
    private static final String TAG = "InPutPayPassWordActivity";
    public Dialog dialog;
    public void showDialog() {

        if (null == dialog) {
            dialog = ShowPopupWindow.unCancleableDialog(this);
        }
        dialog.show();
    }

    public void closeDialog() {
        if (null != dialog) {
            dialog.dismiss();
        }
    }
    private void uploadByxUtils() {
        number = new StringBuffer("");
        for (int i = 0; i < list.size(); i++) {
            number.append(list.get(i));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("orderId", orderid);
        map.put("payType", 5);
        map.put("paypass", number);
        System.out.println(number + "onSuccess: pay");
        showDialog();
        XUtil.Post(ConfigUtil.PAY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                System.out.println(result + "onSuccess: pay");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    String allprice = jsonObject.optString("allprice");
                    String paytype = jsonObject.optString("paytype");
                    String reward = jsonObject.optString("reward");

                    if ("1".equals(res)) {
                        Intent intent = new Intent(InPutPayPassWordActivity.this, SuccessPayActivity.class);
                        intent.putExtra("allprice", allprice);
                        intent.putExtra("paytype", paytype);
                        intent.putExtra("reward", reward);
                        startActivity(intent);
                        Toast.makeText(InPutPayPassWordActivity.this, info, Toast.LENGTH_SHORT).show();
                        finish();
                    } else if ("2".equals(res)) {
                        Toast.makeText(InPutPayPassWordActivity.this, info, Toast.LENGTH_SHORT).show();
                    } else if ("3".equals(res)) {
                        Toast.makeText(InPutPayPassWordActivity.this, info, Toast.LENGTH_SHORT).show();
                    } else if ("4".equals(res)) {
                        Toast.makeText(InPutPayPassWordActivity.this, info, Toast.LENGTH_SHORT).show();
                    } else if ("5".equals(res)) {
                        Toast.makeText(InPutPayPassWordActivity.this, info, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            //1
            case 0:
                setString("1");

                break;

            //2
            case 1:
                setString("2");

                break;

            //3
            case 2:
                setString("3");
                break;

            //4
            case 3:
                setString("4");
                break;

            //5
            case 4:
                setString("5");
                break;

            //6
            case 5:
                setString("6");
                break;

            //7
            case 6:
                setString("7");
                break;

            //8
            case 7:
                setString("8");
                break;

            //9
            case 8:
                setString("9");
                break;

            //0
            case 10:
                setString("0");
                break;

            //删除
            case 11:
                remove();
                break;
        }
        settext(list, old_tv_list);
    }

    private void remove() {
        if (list.size() > 0) {
            list.remove(list.size() - 1);
        }
    }

    private void setString(String s) {
        if (list.size() < 6) {
            list.add(s);
        }
    }

    private void settext(List<String> List, List<TextView> vList) {
        if (List.size() == 0) {
            for (int i = 0; i < vList.size(); i++) {
                vList.get(i).setText("");
            }
        } else if (List.size() == 1) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 1) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 2) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 2) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 3) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 3) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 4) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 4) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 5) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 5) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 6) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 6) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        }
    }

    private void initOldTextview() {
        old_TextView1 = (TextView) old_lay.findViewById(R.id.one_pay_passwork1);
        old_TextView2 = (TextView) old_lay.findViewById(R.id.one_pay_passwork2);
        old_TextView3 = (TextView) old_lay.findViewById(R.id.one_pay_passwork3);
        old_TextView4 = (TextView) old_lay.findViewById(R.id.one_pay_passwork4);
        old_TextView5 = (TextView) old_lay.findViewById(R.id.one_pay_passwork5);
        old_TextView6 = (TextView) old_lay.findViewById(R.id.one_pay_passwork6);
        old_tv_list.add(old_TextView1);
        old_tv_list.add(old_TextView2);
        old_tv_list.add(old_TextView3);
        old_tv_list.add(old_TextView4);
        old_tv_list.add(old_TextView5);
        old_tv_list.add(old_TextView6);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
    }
}
